from django.shortcuts import render
from django.views.generic import ListView,DetailView
from .models import PersonInfo
from django.http import HttpResponse
from django.views.generic.edit import CreateView,UpdateView,DeleteView,FormView
from django.views.generic.dates import MonthArchiveView,WeekArchiveView
from .forms import PersonForm
def home(request):
    context = {
        'person_list_url': '/myapp/persons/',
        'person_create_url': '/myapp/persons/new/',
        'person_detail_url': '/myapp/persons/2/',  # 示例链接，假设有一个ID为1的PersonInfo
        'person_update_url': '/myapp/persons/2/edit/',  # 示例链接，假设有一个ID为1的PersonInfo
        'person_delete_url': '/myapp/persons/2/delete/',  # 示例链接，假设有一个ID为1的PersonInfo
        'person_month_archive_url': '/myapp/persons/month/2024/10/',  # 示例链接，假设要查看2024年10月的档案
        'person_week_archive_url': '/myapp/persons/week/2024/1/',  # 示例链接，假设要查看2024年第42周的档案
    }
    return render(request, 'home.html', context)
class PersonListView(ListView):
    model = PersonInfo
    template_name = 'person_list.html'
    context_object_name = 'person_list'
class PersonDetailView(DetailView):
    model = PersonInfo
    template_name = 'person_detail.html'
    context_object_name = 'person'

class PersonCreateView(CreateView):
    model = PersonInfo
    form_class = PersonForm
    template_name = 'person_form.html'
    success_url = '/myapp/persons/'

    def form_valid(self, form):
        form.save()
        return super().form_valid(form)
class PersonUpdateView(UpdateView):
    model = PersonInfo
    form_class = PersonForm
    template_name = 'person_form.html'
    success_url = '/myapp/persons/'

class PersonDeleteView(DeleteView):
    model = PersonInfo
    template_name = 'person_confirm_delete.html'
    success_url = '/myapp/persons/'

class PersonMonthArchiveView(MonthArchiveView):
    queryset = PersonInfo
    date_field = "hireDate"
    template_name = 'person_archive_month.html'
    year_format = '%Y'
    month_format = '%m'
    paginate_by = 50
    queryset = PersonInfo.objects.all().order_by('hireDate')

class PersonWeekArchiveView(WeekArchiveView):
    queryset = PersonInfo
    date_field = "hireDate"
    template_name = 'person_archive_week.html'
    year_format = '%Y'
    week_format = '%W'
    paginate_by = 50
    queryset = PersonInfo.objects.all().order_by('hireDate')
# Create your views here.


